
********************************************************************************
cd $pathdata_processed_cc
use tirage_temp2008, clear

sum nb_exam 
local min_exam `r(min)'
levelsof nb_exam, c
foreach exam in `r(levels)' {
	cd $pathdata_processed_cc
	use if nb_exam == `exam' using tirage_temp2008, clear 
	
	sum nb_kurs
	local min_kurs `r(min)'
	levelsof nb_kurs, c
	foreach kurs in `r(levels)' {
	    
	cd $pathdata_processed_cc
	use if nb_exam == `exam' & nb_kurs == `kurs' using tirage_temp2008, clear 	

	bysort npid (fagkode): gen subject = _n

	order year_vg3 fylke_vg3 skole_foretak_vg3 group npid subject fagkode skr_hat exam nb_kurs nb_exam

	sum nb_kurs
	local n `r(mean)'
	sum nb_exam
	local k `r(mean)'
	
	gen combinaison = 1
	while `k'>=1 {
	    replace combinaison = combinaison * `n'/`k'
		local k `= `k' -1'
		local n `= `n' -1'		
	}
	
	replace comb = round(comb, 1)
	
	keep year_vg3 fylke_vg3 skole_foretak_vg3 group npid subject fagkode nb_kurs nb_exam combinaison skr_hat exam

	reshape wide fagkode skr_hat exam, i(npid) j(subject)

	expand combinaison
	bysort npid: replace combinaison = _n

	gen tirage = ""
	gen Tirage = ""
	gen Exam = 0
	
	local p = 1

	sum nb_kurs
	local n_kurs `r(mean)'
	
	sum nb_exam
	local n_exam `r(mean)'
	
	if "`n_exam'" == "1" {
	forvalues i = 1(1)`n_kurs' {
	    replace tirage = "`i'" if combinaison == `p'
		replace Tirage = fagkode`i' if combinaison == `p'
		replace Exam = 1 if exam`i' == 1 & combinaison == `p'
		local p `=`p'+1'
		}
	}
	
	if "`n_exam'" == "2" {
	forvalues i = 1(1)`n_kurs' {
		forvalues j = 2(1)`n_kurs' {
		    if `j'>`i' {
				replace tirage = "`i'" + "," + "`j'" if combinaison == `p'
				replace Tirage = fagkode`i' + "," + fagkode`j' if combinaison == `p'
				replace Exam = 1 if exam`i' == 1 & exam`j' == 1 & combinaison == `p'
				local p `=`p'+1'
				}
			}
		}
	}	
	
	if "`n_exam'" == "3" {
	forvalues i = 1(1)`n_kurs' {
		forvalues j = 2(1)`n_kurs' {
			forvalues k = 3(1)`n_kurs' {
				if `j'>`i' & `k'>`j' {
					replace tirage = "`i'" + "," + "`j'" + "," + "`k'" if combinaison == `p'
					replace Tirage = fagkode`i' + "," + fagkode`j' + "," + fagkode`k' if combinaison == `p'
					replace Exam = 1 if exam`i' == 1 & exam`j' == 1 & exam`k' == 1 & combinaison == `p'
					local p `=`p'+1'
					}
				}
			}
		}
	}
	
	if "`n_exam'" == "4" {
	forvalues i = 1(1)`n_kurs' {
		forvalues j = 2(1)`n_kurs' {
			forvalues k = 3(1)`n_kurs' {
			    forvalues l = 4(1)`n_kurs' {
					if `j'>`i' & `k'>`j' & `l'>`k' {
						replace tirage = "`i'" + "," + "`j'" + "," + "`k'" + "," + "`l'" if combinaison == `p'
						replace Tirage = fagkode`i' + "," + fagkode`j' + "," + fagkode`k' + "," + fagkode`l' if combinaison == `p'
						replace Exam = 1 if exam`i' == 1 & exam`j' == 1 & exam`k' == 1 & exam`l' == 1 & combinaison == `p'
						local p `=`p'+1'
						}
					}	
				}
			}
		}
	}
	
	if "`n_exam'" == "5" {
	forvalues i = 1(1)`n_kurs' {
		forvalues j = 2(1)`n_kurs' {
			forvalues k = 3(1)`n_kurs' {
			    forvalues l = 4(1)`n_kurs' {
					forvalues m = 5(1)`n_kurs' {
						if `j'>`i' & `k'>`j' & `l'>`k' & `m'>`l' {
							replace tirage = "`i'" + "," + "`j'" + "," + "`k'" + "," + "`l'" + "," + "`m'" if combinaison == `p'
							replace Tirage = fagkode`i' + "," + fagkode`j' + "," + fagkode`k' + "," + fagkode`l' + "," + fagkode`m' if combinaison == `p'
							replace Exam = 1 if exam`i' == 1 & exam`j' == 1 & exam`k' == 1 & exam`l' == 1 & exam`m' == 1 & combinaison == `p'
							local p `=`p'+1'
							}
						}	
					}	
				}
			}
		}
	}
	
	if "`n_exam'" == "6" {
	forvalues i = 1(1)`n_kurs' {
		forvalues j = 2(1)`n_kurs' {
			forvalues k = 3(1)`n_kurs' {
			    forvalues l = 4(1)`n_kurs' {
					forvalues m = 5(1)`n_kurs' {
						forvalues n = 6(1)`n_kurs' {
							if `j'>`i' & `k'>`j' & `l'>`k' & `m'>`l' & `n'>`m' {
								replace tirage = "`i'" + "," + "`j'" + "," + "`k'" + "," + "`l'" + "," + "`m'" + "," + "`n'" if combinaison == `p'
								replace Tirage = fagkode`i' + "," + fagkode`j' + "," + fagkode`k' + "," + fagkode`l' + "," + fagkode`m' + "," + fagkode`n' if combinaison == `p'
								replace Exam = 1 if exam`i' == 1 & exam`j' == 1 & exam`k' == 1 & exam`l' == 1 & exam`m' == 1 & exam`n' == 1 & combinaison == `p'
								local p `=`p'+1'
								}
							}	
						}	
					}	
				}
			}
		}
	}
	
split tirage, p(,)		
destring tirage*, replace
tostring tirage, replace

foreach i of numlist 1/`n_kurs' {
	gen E`i' = 0
	foreach j of numlist 1/`n_exam' {
		replace E`i' = 1 if tirage`j' == `i'
		}
	}	

gen gpa_c = 0
foreach i of numlist 1/`n_kurs' {
	replace gpa_c = gpa_c + E`i'*(skr_hat`i')
}
replace gpa_c = (gpa_c/(`n_kurs'+`n_exam'))*10

gen predicted_gpa = 0
foreach i of numlist 1/`n_kurs' {
	replace predicted_gpa = predicted_gpa + exam`i'*(skr_hat`i') 
}
replace predicted_gpa = (predicted_gpa/(`n_kurs'+`n_exam'))*10
 
bysort npid (Exam): gen Tirage_final = Tirage[_N]

gen Fagkode = fagkode1
if `n_kurs' >= 2 {
	foreach i of numlist 2/`n_kurs' {
		replace Fagkode	= Fagkode + "," + fagkode`i'
		}
}	
	if "`kurs'" == "`min_kurs'" {
		preserve
	    save comb2008_temp, replace
		restore
	}
	if "`kurs'" != "`min_kurs'" {
		preserve
	    append using comb2008_temp
	    save comb2008_temp, replace
		restore
	}
}

use comb2008_temp, clear
********************************************************************************	
bysort Tirage year_vg3: egen p_c_init = mean(Exam)
bysort npid: egen P_c = sum(p_c_init)
gen p_c = p_c_init/P_c

bysort npid: egen expected_gpa = sum(p_c*gpa_c)

bysort npid: egen variance_gpa = sum(p_c*gpa_c*gpa_c)
replace variance_gpa = variance_gpa - expected_gpa*expected_gpa
replace	variance_gpa = 0 if variance_gpa<0

gen no_var1 = p_c>=1
bysort npid (no_var1): replace no_var1 = no_var1[_N]

foreach var in gpa_c {
	replace `var'= .  if p_c <= 0
	bysort npid `var': gen n`var' = _n == 1
	replace n`var' = 0 if `var' == .
	bysort npid: egen nb`var' = sum(n`var')
}  

foreach var in variance_gpa {
	replace `var' = 0 if no_var1 == 1
	}
replace variance_gpa = 0 if nbgpa_c <= 1

gen non_missing_p_c = !missing(p_c)
bysort npid (non_missing_p_c): replace non_missing_p_c = non_missing_p_c[_N]

ds expected* variance*
foreach var in `r(varlist)' {
		replace `var' = . if non_missing_p_c == 0
}
********************************************************************************	
	if "`exam'" == "`min_exam'" {
		preserve
	    save comb2008, replace
		restore
	}
	if "`exam'" != "`min_exam'" {
		preserve
	    append using comb2008
	    save comb2008, replace
		restore
	}
	
drop p_c_init*		
keep npid group Tirage_final Fagkode expected_gpa predicted_gpa variance_gpa fagkode* p_c*
drop p_c
duplicates drop

reshape long fagkode, i(npid)
drop _j
drop if missing(fagkode)
	
	if "`exam'" == "`min_exam'" {
		preserve
	    save comb22008, replace
		restore
	}
	if "`exam'" != "`min_exam'" {
		preserve
	    append using comb22008
	    save comb22008, replace
		restore
	}
	
keep npid group Tirage_final Fagkode expected_gpa predicted_gpa variance_gpa 
duplicates drop

	if "`exam'" == "`min_exam'" {
		preserve
	    save comb32008, replace
		restore
	}
	if "`exam'" != "`min_exam'" {
		preserve
	    append using comb32008
	    save comb32008, replace
		restore
	}
}
